#!/bin/bash
set -e
# change dir to script location
oldPDW=$(pwd)
cd "${0%/*}"

# config
MAX_R=10

# default arguments
SEED=1
DATASET="ECG200"
TIMEOUT="7200"
CUTOFF=""
METHOD="mo"
ALGO="all"
WRAPPER="./wrapper"

while [[ $# -gt 0 ]]; do
case "$1" in
    --seed|-s)
        SEED="$2"
        shift; shift
        ;;
    --dataset|--data|-d)
        DATASET="$2"
        shift; shift
        ;;
    --testset|--test)
        TESTSET="$2"
        shift; shift
        ;;
    --timeout|--time|-t)
        TIMEOUT="$2"
        shift; shift
        ;;
    --cutoff-time|--cutoff|-c)
        CUTOFF="$2"
        shift; shift
        ;;
    --method|-m)
        METHOD="$2"
        shift; shift
        ;;
    --algorithm|--algo)
        ALGO="$2"
        shift; shift
        ;;
    --instance-file|--inst|-i)
        INSTANCE_FILE="$2"
        shift; shift
        ;;
    --batch)
        BATCH="true"
        shift
        ;;
    *)
        echo "ignoring unknown argument \"$1\""
        shift
        ;;
esac
done

SMAC="python ../smac/smac \
        --verbose_level DEBUG \
        --maxR ${MAX_R}"
PARAMILS="../paramils/paramils \
        --validation false \
        --MO true \
        --rungroup run_${SEED} \
        --log-details true \
        --max-runs ${MAX_R}"

# for stand-alone use
if [ -z "$BATCH" ]; then
    NFOLDS=5
    DATANAME=$(basename $DATASET)
    DATANAME=${DATANAME%.*}
    OUTPATH="$TMP/MultiETSC/${DATANAME}-{}/"
    INSTANCE_FILE="$TMP/MultiETSC/${DATANAME}-$SEED/train_list.txt"
    if ! python validationsplitter.py --folds=$NFOLDS  --seed=$SEED --outpath="$OUTPATH" "${oldPDW}/${DATASET}" &> /dev/null; then
        printf "%20s: \e[31mFailed to split dataset.\e[0m\n" $DATASET
        printf "Not sufficient training data to create a $NFOLDS-fold split\n"
        exit 1
    fi
else
    DATANAME="$DATASET"
fi

if [[ $METHOD == "so" ]]; then
    CONFIGURATOR="${SMAC}"
    WRAPPER="./wrapper --MO false"
else
    CONFIGURATOR="${PARAMILS}"
fi

if [ ! -z "$BATCH" ]; then
    CONDITION="${DATANAME}/${METHOD}-${ALGO}"
    CONF_OUT="../experiments/output/configurator/${CONDITION}"
else
    CONDITION="${DATANAME}"
    CONF_OUT="../MultiETSC/output/configurator/${CONDITION}"
fi
mkdir -p "${CONF_OUT}"

# Start the configurator
${CONFIGURATOR} --scenario "scenario.txt" \
                --algo "$WRAPPER" \
                --seed "$SEED" \
                --instance-file "$INSTANCE_FILE" \
                --wallclock-limit "$TIMEOUT" \
                --cutoff-time "$CUTOFF" \
                --deterministic 0 \
                --run-obj "quality" \
                --overall-obj "mean" \
                --execdir "../MultiETSC/" \
                --output-dir "${CONF_OUT}" \
                --param-file "pcs/${METHOD}-${ALGO}.pcs"

# make sure the configs file is populated
CONFIGS="${CONF_OUT}/run_${SEED}/configs${SEED}.txt"
if [[ $METHOD == "so" ]]; then
    ../smac/configs.py "${CONF_OUT}/run_${SEED}/traj.json" > "${CONFIGS}"
    mv "${CONF_OUT}/run_${SEED}.OLD/log" "${CONF_OUT}/run_${SEED}"
    rm -rf ${CONF_OUT}/run_${SEED}.OLD
fi

if [ ! -z "$TESTSET" ]; then
    echo "Running test evaluation:"
    echo "Result: status, time, [earliness, error rate], 0, configuration"
    INSTANCE="${oldPDW}/${DATASET}:${oldPDW}/${TESTSET}"
    while read CONF; do
        echo "$(./wrapper --MO "true" $INSTANCE 0 900 0 $SEED $CONF), $CONF"
    done < $CONFIGS
fi
